package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.RoleAuthService;
import com.tzbank.entity.backstage.RoleAuth;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class RoleAuthController {

	@Autowired
	private RoleAuthService roleAuthService;

	/**
	 * 添加角色关联权限
	 * @param request
	 * @param roleAuth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "insertRoleAuth")
	public Map<String, Object> insertRoleAuth(HttpServletRequest request, @RequestBody RoleAuth roleAuth) {
		try {
			return this.roleAuthService.insertRoleAuth(roleAuth);
		} catch (Exception e) {
			log.error("RoleAuthController insertRoleAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除角色关联权限
	 * @param request
	 * @param roleAuth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "deleteRoleAuth")
	public Map<String, Object> deleteRoleAuth(HttpServletRequest request, @RequestBody RoleAuth roleAuth) {
		try {
			return this.roleAuthService.deleteRoleAuth(roleAuth);
		} catch (Exception e) {
			log.error("RoleAuthController deleteRoleAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改角色关联权限
	 * @param request
	 * @param roleAuth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "updateRoleAuth")
	public Map<String, Object> updateRoleAuth(HttpServletRequest request, @RequestBody RoleAuth roleAuth) {
		try {
			return this.roleAuthService.updateRoleAuth(roleAuth);
		} catch (Exception e) {
			log.error("RoleAuthController updateRoleAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 通过自增主键查询角色关联权限
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectRoleAuthByAutoId")
	public Map<String, Object> selectRoleAuthByAutoId(HttpServletRequest request,
			@RequestParam(required = true) Integer autoId) {
		try {
			return this.roleAuthService.selectRoleAuthByAutoId(autoId);
		} catch (Exception e) {
			log.error("RoleAuthController selectRoleAuthByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询角色关联权限
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectRoleAuthList")
	public Map<String, Object> selectRoleAuthList(HttpServletRequest request) {
		try {
			return this.roleAuthService.selectRoleAuthList();
		} catch (Exception e) {
			log.error("RoleAuthController selectRoleAuthList error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
